﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace landlord2
{
    /// <summary>
    /// CardGroupBack.xaml 的交互逻辑
    /// </summary>
    public partial class CardGroupBack : UserControl
    {
        private static readonly Size CardSize = new Size(73, 98);
        private static readonly int CardMargin = 10;
        private static readonly int ShaddowSize = 6;


        public CardGroupBack()
        {
            InitializeComponent();
        }

        public static readonly DependencyProperty DirectionProperty = DependencyProperty.Register("Direction", typeof(CardGroupDirection), typeof(CardGroupBack),
            new FrameworkPropertyMetadata(CardGroupDirection.Horizontal));

        public CardGroupDirection Direction
        {
            get
            {
                return (CardGroupDirection)GetValue(DirectionProperty);
            }
            set
            {
                SetValue(DirectionProperty, value);
            }
        }

        public static readonly DependencyProperty CardStyleProperty = DependencyProperty.Register("CardStyle", typeof(int), typeof(CardGroupBack),
            new FrameworkPropertyMetadata(0, OnCardStylePropertyChanged));

        public int CardStyle
        {
            get
            {
                return (int)GetValue(CardStyleProperty);
            }
            set
            {
                SetValue(CardStyleProperty, value);
            }
        }

        private static void OnCardStylePropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            CardGroupBack cardGroup = (CardGroupBack)obj;
            cardGroup.ShowBack();
        }

        public void AddCards(int count)
        {
            for (int i = 0; i < count; i++)
            {
                Image img = NewCardImage();
                gridContent.Children.Add(img);
            }
            ReLayout();
        }

        public void RemoveCards(int count)
        {
            for (int i = 0; i < count; i++)
                gridContent.Children.RemoveAt(gridContent.Children.Count - 1);
            ReLayout();
        }

        public void ClearCards()
        {
            gridContent.Children.Clear();
            ReLayout();
        }

        public int GetCardCount()
        {
            return gridContent.Children.Count;
        }

        private Image NewCardImage()
        {
            Image img = new Image();
            img.Style = (Style)App.Current.Resources["CardStyle"];
            img.HorizontalAlignment = HorizontalAlignment.Left;
            img.VerticalAlignment = VerticalAlignment.Top;
            img.Source = (BitmapSource)App.Current.Resources[string.Format("{0}_Back", GetCardStyleString(CardStyle))];
            return img;
        }

        public void ShowBack()
        {
            foreach (UIElement el in gridContent.Children)
            {
                Image img = (Image)el;
                img.Source = (BitmapSource)App.Current.Resources[string.Format("{0}_Back", GetCardStyleString(CardStyle))];
            }
        }

        private void ReLayout()
        {
            UIElementCollection coll = gridContent.Children;
            if (coll.Count == 0)
            {
                Width = ShaddowSize;
                Height = ShaddowSize;
                return;
            }

            if (Direction == CardGroupDirection.Horizontal)
            {
                int y = 0;
                int needWidth = (coll.Count - 1) * CardMargin + (int)CardSize.Width;
                int x = 0;
                Width = needWidth + ShaddowSize;
                Height = (int)CardSize.Height + ShaddowSize;
                int i = 0;
                foreach (UIElement el in coll)
                {
                    Image img = (Image)el;
                    Thickness thick = img.Margin;
                    thick.Left = x;
                    thick.Top = y;
                    img.Margin = thick;
                    Panel.SetZIndex(img, i);
                    i++;
                    x += CardMargin;
                }
            }
            else
            {
                int x = 0;
                int needHeight = (coll.Count - 1) * CardMargin + (int)CardSize.Height;
                int y = 0;
                Width = (int)CardSize.Width + ShaddowSize;
                Height = needHeight + ShaddowSize;
                int i = 0;
                foreach (UIElement el in coll)
                {
                    Image img = (Image)el;
                    Thickness thick = img.Margin;
                    thick.Left = x;
                    thick.Top = y;
                    img.Margin = thick;
                    Panel.SetZIndex(img, i);
                    i++;
                    y += CardMargin;
                }
            }
        }

        private string GetCardStyleString(int cardStyle)
        {
            switch (CardStyle)
            {
                case 0: return "S1";
                case 1: return "S2";
                case 2: return "S3";
                case 3: return "S4";
                default: throw new Exception("Invalid Card Style!");
            }
        }
    }

    public enum CardGroupDirection
    {
        Horizontal,
        Vertical
    }
}
